﻿#include "EditorWelcomePageView.h"

#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/ImageWidget.h"

#include "Asset/EditorDefaultPackage.h"


void REditorWelcomePageView::BindViewModel(TSharedObjectPtr<REditorPageViewModel> InViewModel)
{
    ViewModel =InViewModel;
}

void REditorWelcomePageView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    TSharedObjectPtr<RVerticalPositionerWidget> Main=NewWidget<RVerticalPositionerWidget>(InParent);
    Main->Anchor.CenterIn(InParent);
    Main->Width=300;
    Main->Height=300;
    Main->Spacing=5;

    TSharedObjectPtr<RTextWidget> Title=NewWidget<RTextWidget>(Main);
    Title->Text=U"引擎编辑器";
    Title->FontSize=20;
    Title->Color=SColor::FromHexRGB(0x868686);
    Title->Height=35;

    TSharedObjectPtr<RTextWidget> Version=NewWidget<RTextWidget>(Main);
    Version->Text=U"版本: 1.0.0";
    Version->FontSize=12;
    Version->Color=SColor::FromHexRGB(0x868686);
    Version->Height=30;

    TSharedObjectPtr<RTextWidget> Start=NewWidget<RTextWidget>(Main);
    Start->Text=U"启动";
    Start->FontSize=16;
    Start->Color=SColor::FromHexRGB(0x868686);
    Start->Height=30;

    const float IconSize=28;

    //add NewProject option 
    TSharedObjectPtr<RMouseWidget> NewProject=NewWidget<RMouseWidget>(Main);
    NewProject->Height=30;
    {
        auto Icon=NewWidget<RImageWidget>(NewProject);
        Icon->Image=REditorDefaultPackage::Get()->GetIcon(U"CodiconNewFile_Blue");
        Icon->Width=IconSize;
        Icon->Height=IconSize;
        Icon->Anchor.LeftReference=NewProject;
        Icon->Anchor.BottomReference=NewProject;

        auto Text=NewWidget<RTextWidget>(NewProject);
        Text->Text=U"新建项目";
        Text->FontSize=16;
        Text->Color=SColor::FromHexRGB(0x0078D4);
        Text->Anchor.LeftReference=Icon;
        Text->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        Text->Anchor.LeftOffset=5;
        Text->Anchor.BottomReference=NewProject;
        Text->VerticalAlignment=ETextVerticalAlignment::Bottom; 
    }
    NewProject->OnClicked.BindLambda([this](const SMouseButtonEvent&){
        ViewModel->NewProject();
        return true;
    });


    //add OpenProject option 
    TSharedObjectPtr<RMouseWidget> OpenProject=NewWidget<RMouseWidget>(Main);
    OpenProject->Height=30;
    {
        auto Icon=NewWidget<RImageWidget>(OpenProject);
        Icon->Image=REditorDefaultPackage::Get()->GetIcon(U"CodiconGoToFile_Blue");
        Icon->Width=IconSize;
        Icon->Height=IconSize;
        Icon->Anchor.LeftReference=OpenProject;
        Icon->Anchor.BottomReference=OpenProject;

        auto Text=NewWidget<RTextWidget>(OpenProject);
        Text->Text=U"打开项目";
        Text->FontSize=16;
        Text->Color=SColor::FromHexRGB(0x0078D4);
        Text->Anchor.LeftReference=Icon;
        Text->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        Text->Anchor.LeftOffset=5;
        Text->Anchor.BottomReference=OpenProject;
        Text->VerticalAlignment=ETextVerticalAlignment::Bottom; 
    }
    OpenProject->OnClicked.BindLambda([this](const SMouseButtonEvent&){
        ViewModel->OpenProject();
        return true;
    });

    TSharedObjectPtr<RTextWidget> Recent=NewWidget<RTextWidget>(Main);
    Recent->Text=U"最近";
    Recent->FontSize=16;
    Recent->Color=SColor::FromHexRGB(0x868686);
    Recent->Height=30;

    //add RecentProjects
    for(int i=0;i<ViewModel->GetRecentProjectNum();++i)
    {
        TSharedObjectPtr<RMouseWidget> RecentProject=NewWidget<RMouseWidget>(Main);
        RecentProject->Height=30;
        {
            //just show the name and project path
            auto Name=NewWidget<RTextWidget>(RecentProject);
            Name->Text=ViewModel->GetRecentProjectName(i);
            Name->FontSize=16;
            Name->Color=SColor::FromHexRGB(0x0078D4);
            Name->Anchor.LeftReference=RecentProject;
            Name->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Left;
            Name->Anchor.LeftOffset=5;
            RTextWidgetWeakPtr WeakName=Name;
            Name->Width=[WeakName](){
                return WeakName->GetTextSize().X+20;
            };
            Name->Anchor.BottomReference=RecentProject;
            Name->VerticalAlignment=ETextVerticalAlignment::Bottom;

            auto Path=NewWidget<RTextWidget>(RecentProject);
            Path->Text=ViewModel->GetRecentProjectPath(i);
            Path->FontSize=16;
            Path->Color=SColor::FromHexRGB(0x868686);
            Path->Anchor.LeftReference=Name;
            Path->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
            Path->Anchor.LeftOffset=5;
            Path->Anchor.BottomReference=RecentProject;
            Path->VerticalAlignment=ETextVerticalAlignment::Bottom;

        }
        RecentProject->OnClicked.BindLambda([i,this](const SMouseButtonEvent&){
            ViewModel->OpenRecentProject(i);
            return true;
        });
    }

}
